package pl.shenlon.poker.cards.deck;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import pl.shenlon.poker.cards.Card;
import pl.shenlon.poker.cards.CardRank;
import pl.shenlon.poker.cards.CardSuit;

public class Deck {
	private final Card[] cards;

	public final int length;

	private Queue<Card> shuffledDeck;

	public Deck(Card... cards) {
		this.cards = cards;
		length = cards.length;
	}

	public Deck(CardSuit[] suits, CardRank[] ranks) {
		cards = new Card[suits.length * ranks.length];
		int position = 0;
		for (CardSuit suit : suits) {
			for (CardRank rank : ranks) {
				cards[position] = new Card(suit, rank);
				position++;
			}
		}
		length = cards.length;
	}

	public Card getCardAtPosition(int position) {
		return cards[position];
	}
	
	public Card[] getCardsInDeck() {
		return cards;
	}

	public int getCardsInDeckCount() {
		return cards.length;
	}

	public int getCardsToDealCount() {
		return shuffledDeck.size();
	}

	public Card next() {
		if (shuffledDeck == null) {
			throw new IllegalStateException(
					"Trying to get access to deck before shuffling. Use shuffle() first");
		}
		return shuffledDeck.poll();
	}

	public void shuffle() {
		List<Card> cardsList = java.util.Arrays.asList(cards);
		Collections.shuffle(cardsList);
		shuffledDeck = new LinkedList<Card>();
		for (Card card : cardsList) {
			shuffledDeck.add(card);
		}
	}

}